<?php

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Thu, 1 Jan 1970 01:00:00 GMT"); // Date in the past
header('Content-type: text/javascript');
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
ini_set('log_errors', '1');
ini_set('error_log', 'php_error.log');
ini_set('display_errors', '1');

$appIdTMT = "140410776032411";
$appSecret = "fb2894b6ece45f1371ce316818e94a88";

$appId = "55374793287";
$appSecret = "c685ea6a9ea989eff3e5857f0ae7409b";

$host = $_SERVER['HTTP_HOST'];
$uri = trim(dirname($_SERVER['PHP_SELF']), '/\\');

$message = !empty($_REQUEST['message']) ? $_REQUEST['message'] : 'application data';
$callback = !empty($_REQUEST['callback']) ? $_REQUEST['callback'] : 'fbAuthCallback';
$access_token = !empty($_REQUEST['access_token']) ? $_REQUEST['access_token'] : '1';
$id = !empty($_REQUEST['id']) ? $_REQUEST['id'] : '1';
$connection = !empty($_REQUEST['connection']) ? $_REQUEST['connection'] : '';
$jsonData = !empty($_REQUEST['jsonData']) ? $_REQUEST['jsonData'] : '{---}';

$size = 0;
$maxSize = 65833;

//upload photo
$file = "yoshi1.gif";
//$file = 'C:\Users\nathan\Documents\Visual Studio 2010\Projects\Nexus\Yoshi\ImageWork\logo.gif';

//$jsonData = "@@JSON@@" . $jsonData . "@@JSON@@";
$message = $jsonData;

//file_put_contents("comment.txt", $jsonData);
//$result = exec("mogrify -comment @comment.txt $file");

//$commentFile = "big.txt";
//$handle = fopen($commentFile, "r+");
//ftruncate($handle, 64982);
//fflush($handle);
//fclose($handle);
//$message = file_get_contents($commentFile);

$size = strlen($message);


$args = array(
    'message' => $message,
	'name' => $message
);
$args[basename($file)] = '@' . realpath($file);
$ch = curl_init();
//$url = 'https://graph.facebook.com/' . $id . '/photos?access_token=' . $access_token;
$url = 'https://graph.facebook.com/' . $id . $connection . '?access_token=' . $access_token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
if ($cookies != '') {
	if (substr(PHP_OS, 0, 3) == 'WIN') {
		$cookies = str_replace('\\', '/', getcwd() . '/' . $cookies);
	}
	curl_setopt($ch, CURLOPT_COOKIEJAR, $cookies);
	curl_setopt($ch, CURLOPT_COOKIEFILE, $cookies);
}
$data = curl_exec($ch);

if ($callback)
	$ot .= "$callback({\n";
else
	$ot .= "{\n";
$ot .= '"errno":' . json_encode(curl_errno($ch)) . ",\n";
$ot .= '"error":' . json_encode(curl_error($ch)) . ",\n";
$ot .= '"url":' . json_encode($url) . ",\n";
$ot .= '"content":' . json_encode($data) . ",\n";
$ot .= '"return":' . json_encode(print_r($data, true)) . ",\n";
$ot .= '"mogrify result":' . json_encode($result) . ",\n";
$ot .= '"file":' . json_encode(realpath($file)) . ",\n";
$ot .= '"message":' . json_encode($message) . ",\n";
$ot .= '"cwd":' . json_encode(getcwd()) . ",\n";
$ot .= '"root":' . json_encode($_ENV['DOCUMENT_ROOT']) . ",\n";
$ot .= '"size":' . json_encode($size) . ",\n";
if ($callback)
	$ot .= "})";
else
	$ot .= "}";
echo $ot;
exit(0);

//returns the photo id
echo "Data result\n";
echo "errno=" . curl_errno($ch) . "\n";
echo "error=" . curl_error($ch) . "\n";
echo "---------------------------------------------------------------------------------------------\n";
echo print_r(json_decode($data, true), true);
echo "---------------------------------------------------------------------------------------------\n";
echo print_r($data, true) . "\n";
echo "---------------------------------------------------------------------------------------------\n";
echo "url = $url\n";
echo "---------------------------------------------------------------------------------------------\n";
echo print_r($args, true);
// ?>